package com.ll.micrograss.grass.controller;


import cn.hutool.core.util.RandomUtil;
import com.ll.api.feign.PointFeign;
import com.ll.common.core.common.R;
import com.ll.common.minio.utils.MinioUtils;
import com.ll.micrograss.grass.pojo.DTO.GrassDTO;
import com.ll.micrograss.grass.service.IGrassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/article")
public class GrassController {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private IGrassService grassService;



    /**
     * 图片上传
     */
    @RequestMapping("/upload/img")
    public R upload(MultipartFile file) throws IOException {
        // 生成随机文件名10位
        String prefix = RandomUtil.randomString(10);
        // 获取文件后缀
        String[] split = file.getOriginalFilename().split("\\.");
        String suffix = split[split.length - 1];
        // 上传文件,对文件名(文件名+后缀)进行拼接
        String upload = minioUtils.upload(prefix + "." + suffix, file.getInputStream());
        return R.ok(upload);
    }

    /**
     * 添加文章
     */
    @RequestMapping("/add")
    public R add(@RequestBody GrassDTO grassDTO){
        grassService.add(grassDTO);
        return R.ok();
    }
}
